﻿<%@ Page Title="Cross-Origin Resource Sharing in ASP.NET WebForms" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Cors.aspx.cs" Inherits="Cors.Origin.Cors" %>
<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server">
    <div id="divTarget"></div>
</asp:Content>
<asp:Content ID="ScriptContent" ContentPlaceHolderID="ScriptContent" runat="server">
    <script type="text/javascript">
        (function () {
            var request = false;
            try {
                //Create XMLHttpRequest and assing it to 'request' variable
                try {
                    request = new ActiveXObject('Msxml2.XMLHTTP');
                } catch (e) {
                    try {
                        request = new ActiveXObject('Microsoft.XMLHTTP');
                    } catch (e) {
                        request = false;
                    }
                }
                if (!request && typeof XMLHttpRequest != 'undefined') {
                    try {
                        request = new XMLHttpRequest();
                    } catch (e) {
                        request = false;
                    }
                }
                if (!request && window.createRequest) {
                    try {
                        request = window.createRequest();
                    } catch (e) {
                        request = false;
                    }
                }

                if (request) {
                    var jsonUrl = 'http://localhost:60000/cors';
                    //Check if XMLHttpRequest support CORS
                    if (request.withCredentials !== 'undefined') {
                        //Perform request
                        request.open('GET', jsonUrl, true);
                        request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
                        request.onreadystatechange = function () {
                            if (request.readyState == 4) {
                                if (request.status == 200) {
                                    document.getElementById('divTarget').innerHTML = request.responseText;
                                }
                            }
                        };
                        //If not, than check if XDomainRequest is available
                    } else if (typeof XDomainRequest != 'undefined') {
                        //Perform request
                        request = new XDomainRequest();
                        request.open('GET', jsonUrl);
                        request.onload = function () {
                            document.getElementById('divTarget').innerHTML = request.responseText;
                        };
                    } else {
                        throw (null);
                    }
                } else {
                    throw (null);
                }

                request.send(null);
            } catch (e) {
                //There is no support for CORS, use something else instead (for example JSONP)
            }
        })();
    </script>
</asp:Content>

